home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / StdPrefs 1.0 / StdPrefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  3.2 KB  |  117 lines  |  [TEXT/MPS ]

  1.  
  2. /* File StdPrefs.c
  3.     Standard preferences file handling routines, implementation.
  4.     System 6 or better requires HFS
  5.     Copyright (c) 1996, 1997 by John Montbriand.  All Rights Reserved.
  6.     Permission hereby granted for public use.
  7.         Distribute freely in areas where the laws of copyright apply.
  8.     USE AT YOUR OWN RISK.
  9.     DO NOT DISTRIBUTE MODIFIED COPIES.
  10.     Comments/questions/postcards* to the author at the address:
  11.       John Montbriand
  12.       P.O. Box. 1133
  13.       Saskatoon Saskatchewan Canada
  14.       S7K 3N2
  15.     or by email at:
  16.       tinyjohn@sk.sympatico.ca
  17.     *if you mail a postcard, then I will provide you with technical support
  18.     regarding questions you may have about this file.
  19.           see also: <http://www3.sk.sympatico.ca/tinyjohn>
  20. */
  21.  
  22. #include "StdPrefs.h"
  23. #include <Folders.h>
  24. #include <OSUtils.h>
  25. #include <Resources.h>
  26. #include <Gestalt.h>
  27. #include <String.h>
  28. #include <PLStringFuncs.h>
  29. #include <Errors.h>
  30. #include <TextUtils.h>
  31.  
  32.  
  33. OSErr FindPreferencesFolder(short *pvol, long *pdir) {
  34.     OSErr err;
  35.     long response = 0;
  36.     *pvol = 0;
  37.     *pdir = 0;
  38.     if (Gestalt(gestaltFindFolderAttr, &response) != noErr) response = 0;
  39.     if ((response & (1<<gestaltFindFolderPresent)) != 0) {
  40.         err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, pvol, pdir);
  41.     } else {
  42.         long procID;
  43.         SysEnvRec the_env;
  44.         SysEnvirons(curSysEnvVers, &the_env);
  45.         err = GetWDInfo(the_env.sysVRefNum, pvol, pdir, &procID);
  46.     }
  47.     return err;
  48. }
  49.  
  50.  
  51. OSErr PrefsDirSearch(OSType creator, OSType type, short *fvol, long *fdir, StringPtr fname) {
  52.     short vol;
  53.     long dir;
  54.     CInfoPBRec cat;
  55.     FInfo *fin;
  56.     Str255 name;
  57.     OSErr err;
  58.  
  59.         /* find the preferences folder */
  60.      err = FindPreferencesFolder(&vol, &dir);
  61.     if (err != noErr) return err;
  62.  
  63.         /* look for the file */
  64.     memset(&cat, 0, sizeof(cat));
  65.     cat.hFileInfo.ioNamePtr = name;
  66.     cat.hFileInfo.ioVRefNum = vol;
  67.     cat.hFileInfo.ioDirID = dir;
  68.     cat.hFileInfo.ioFDirIndex = 1;
  69.     fin = &cat.hFileInfo.ioFlFndrInfo;
  70.     while (PBGetCatInfoSync(&cat) == noErr) {
  71.         if ((cat.hFileInfo.ioFlAttrib & 16) == 0
  72.         && fin->fdType == type 
  73.         && fin->fdCreator == creator) {
  74.             PLstrcpy(fname, name);
  75.             *fvol = vol;
  76.             *fdir = dir;
  77.             return noErr;
  78.         }
  79.         cat.hFileInfo.ioDirID = dir;
  80.         cat.hFileInfo.ioFDirIndex++;
  81.     }
  82.     return fnfErr;
  83. }
  84.  
  85. OSErr FSpPrefsDirSearch(OSType creator, OSType type, FSSpec *spec) {
  86.     return PrefsDirSearch(creator, type, &spec->vRefNum, &spec->parID, spec->name);
  87. }
  88.  
  89. OSErr PrefsCreate(OSType creator, OSType type, StringPtr defaultname, short *fvol, long *fdir, StringPtr fname) {
  90.     OSErr err;
  91.     long num;
  92.     FInfo fndrInfo;
  93.     Str255 s;
  94.     num = 1;
  95.     if ((err = FindPreferencesFolder(fvol, fdir)) != noErr) goto bail;
  96.     PLstrcpy(fname, defaultname);
  97.     while (1) {
  98.         err = HGetFInfo(*fvol, *fdir, fname, &fndrInfo);
  99.         if (err == fnfErr) break; else if (err != noErr) goto bail;
  100.         PLstrcpy(fname, defaultname);
  101.         PLstrcat(fname, "\p "); /* followed by a space */
  102.         NumToString(num++, s);
  103.         PLstrcat(fname, s); /* and a number */
  104.     }
  105.     err = HCreate(*fvol, *fdir, fname, creator, type);
  106.     if (err != noErr) goto bail;
  107.     HCreateResFile(*fvol, *fdir, fname);
  108.     if ((err = ResError()) != noErr) goto bail;
  109.     return noErr;
  110. bail:
  111.     return err;
  112. }
  113.  
  114. OSErr FSpPrefsCreate(OSType creator, OSType type, StringPtr defaultname, FSSpec *spec) {
  115.     return PrefsCreate(creator, type, defaultname, &spec->vRefNum, &spec->parID, spec->name);
  116. }
  117.